package com.xxy.hashtable;

import com.sun.xml.internal.ws.addressing.WsaActionUtil;

import java.nio.charset.IllegalCharsetNameException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Scanner;

/**
 * @author xxy
 * @create 2020 12 18 14:07
 */
public class HashTabDemo {
    public static void main(String[] args) {
        HashTab hashtable = new HashTab(7);
        String key="";
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("add:添加员工");
            System.out.println("list:显示员工");
            System.out.println("exit:退出系统");
            key =scanner.next();
            switch (key){
                case "add":
                    System.out.println("输入id:");
                    int id =scanner.nextInt();
                    System.out.println("输入姓名：");
                    String name =scanner.next();
                    Emp emp =new Emp(id,name);
                    hashtable.add(emp);
                    break;
                case "list":
                    hashtable.list();
                    break;
                case "find":
                    System.out.println("输入要查找的id：");
                    int i =scanner.nextInt();
                    hashtable.findEmpById(i);
                case "del":
                    System.out.println("请输入要删除的id");
                case "exit":
                    scanner.close();
                    System.exit(0);
                default:
                    break;
            }
        }

    }
}
//员工信息
class Emp{
   private int id;
   private String name;
   public Emp next;
    public Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Emp getNext() {
        return next;
    }
    public void setNext(Emp next) {
        this.next = next;
    }
}
class EmpLinkedList{
    private Emp head;//头指针
    //添加员工到链表
    public void add(Emp emp){
        //该链表的第一个员工
        if(head==null){
            head=emp;
            return;
        }
        //不是第一个员工
        Emp flag =head;
        while(true){
            if(flag.next==null){
                //找到位置
                flag.next=emp;
                break;
            }
            flag=flag.next;
        }
    }
    //遍历当前链表
    public void list(){
        //判断有没有有员工信息
        if(head==null){
            System.out.println("没有员工");
            return;
        }
        Emp flag = head;
        while (true){
            System.out.println("id"+flag.getId()+"姓名"+flag.getName());
            if (flag.next == null) {
                //到了最后，结束
                return;
            }
            flag=flag.next;
        }
    }
    //查找员工
    public Emp findEmpById(int id){
        if(head==null){
            System.out.println("链表空");
            return null;
        }
        Emp flag = head;
        while (true){
            if(flag.getId()==id){
                //找到
                return flag;
            }
            if(flag.next== null){
                System.out.println("没有找到");
                break;
            }
            flag=flag.next;
        }
        return null;
    }
    //删除员工
    public void del(int id){

    }
}
class  HashTab{
    private EmpLinkedList[] empLinkedLists;
    private int size;
    public HashTab(int size) {
        this.size = size;
        empLinkedLists =new EmpLinkedList[size];
        for(int i =0;i<size;i++){
            empLinkedLists[i] =new EmpLinkedList();
        }
    }
    public int hashFun(int id){
        return id%size;
    }
    //添加
    public void add(Emp emp){
        int number = hashFun(emp.getId());
        empLinkedLists[number].add(emp);
    }
    //遍历
    public void list(){
        for(int i=0;i<size;i++){
            empLinkedLists[i].list();
        }
    }
    //查找
    public void findEmpById(int id){
        int number =hashFun(id);
       Emp emp= empLinkedLists[number].findEmpById(id);
       if(emp!=null){
           System.out.println(emp.getId()+" "+emp.getName());
       }
    }
}
